package com.ywj.client;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.websocket.*;

/**
 * 测试客户端
 */

public class Client {

    static Session session;
    public static void main(String[] args) throws Exception{
        try {
            //调用javax.websocket.ContainerProvider提供的getWebSocketContainer()方法获取一个WebSocketContainer对象
            //使用它的connectServer方法连接到WebSocket服务器
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
//            String uri = "ws://localhost:8080/websocket";
            String uri = "ws://localhost:8888";
            System.out.println("try to connect to " + uri);
            System.out.println("session1:   " + session);     //session1为 null

            session = container.connectToServer(MyClientEndpoint.class, URI.create(uri));

            System.out.println("session2    " + session);     //非常长，和MyClientEndpoint中的session同
            System.out.println("session2    " + session.getId());
//            System.out.println("session.getBasicRemote():   " + session.getBasicRemote());   //Wrapped: Basic
//            System.out.println("session.getAsyncRemote():   " + session.getAsyncRemote());   //Wrapped: Async

//            byte[] bis = Files.readAllBytes(Paths.get("C:\\Users\\28687\\Desktop\\xshell基本操作.txt"));
//            session.getBasicRemote().sendText(new String(bis));
            session.getBasicRemote().sendText("Hello, I am client, my session is : " + session.getId());


            java.io.BufferedReader r=new  java.io.BufferedReader(new java.io.InputStreamReader( System.in));
            while(true){
//                session.add
                String line=r.readLine();
                if(line.equals("quit")) break;
                session.getBasicRemote().sendText(line);
            }

        } catch ( Exception ex) {
            ex.printStackTrace();
        }
    }
}